home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / BasicHLSL / BasicHLSL.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  5.8 KB  |  163 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: BasicHLSL.fx
  3. //
  4. // The effect file for the BasicHLSL sample.  
  5. // 
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8.  
  9.  
  10. //--------------------------------------------------------------------------------------
  11. // Global variables
  12. //--------------------------------------------------------------------------------------
  13. float4 g_MaterialAmbientColor;      // Material's ambient color
  14. float4 g_MaterialDiffuseColor;      // Material's diffuse color
  15. int g_nNumLights;
  16.  
  17. float3 g_LightDir[3];               // Light's direction in world space
  18. float4 g_LightDiffuse[3];           // Light's diffuse color
  19. float4 g_LightAmbient;              // Light's ambient color
  20.  
  21. texture g_MeshTexture;              // Color texture for mesh
  22.  
  23. float    g_fTime;                   // App's time in seconds
  24. float4x4 g_mWorld;                  // World matrix for object
  25. float4x4 g_mWorldViewProjection;    // World * View * Projection matrix
  26.  
  27.  
  28.  
  29. //--------------------------------------------------------------------------------------
  30. // Texture samplers
  31. //--------------------------------------------------------------------------------------
  32. sampler MeshTextureSampler = 
  33. sampler_state
  34. {
  35.     Texture = <g_MeshTexture>;
  36.     MipFilter = LINEAR;
  37.     MinFilter = LINEAR;
  38.     MagFilter = LINEAR;
  39. };
  40.  
  41.  
  42. //--------------------------------------------------------------------------------------
  43. // Vertex shader output structure
  44. //--------------------------------------------------------------------------------------
  45. struct VS_OUTPUT
  46. {
  47.     float4 Position   : POSITION;   // vertex position 
  48.     float4 Diffuse    : COLOR0;     // vertex diffuse color (note that COLOR0 is clamped from 0..1)
  49.     float2 TextureUV  : TEXCOORD0;  // vertex texture coords 
  50. };
  51.  
  52.  
  53. //--------------------------------------------------------------------------------------
  54. // This shader computes standard transform and lighting
  55. //--------------------------------------------------------------------------------------
  56. VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
  57.                          float3 vNormal : NORMAL,
  58.                          float2 vTexCoord0 : TEXCOORD0,
  59.                          uniform int nNumLights,
  60.                          uniform bool bTexture,
  61.                          uniform bool bAnimate )
  62. {
  63.     VS_OUTPUT Output;
  64.     float3 vNormalWorldSpace;
  65.   
  66.     float4 vAnimatedPos = vPos;
  67.     
  68.     // Animation the vertex based on time and the vertex's object space position
  69.     if( bAnimate )
  70.         vAnimatedPos += float4(vNormal, 0) * (sin(g_fTime+5.5)+0.5)*5;
  71.     
  72.     // Transform the position from object space to homogeneous projection space
  73.     Output.Position = mul(vAnimatedPos, g_mWorldViewProjection);
  74.     
  75.     // Transform the normal from object space to world space    
  76.     vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)
  77.     
  78.     // Compute simple directional lighting equation
  79.     float3 vTotalLightDiffuse = float3(0,0,0);
  80.     for(int i=0; i<nNumLights; i++ )
  81.         vTotalLightDiffuse += g_LightDiffuse[i] * max(0,dot(vNormalWorldSpace, g_LightDir[i]));
  82.         
  83.     Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse + 
  84.                          g_MaterialAmbientColor * g_LightAmbient;   
  85.     Output.Diffuse.a = 1.0f; 
  86.     
  87.     // Just copy the texture coordinate through
  88.     if( bTexture ) 
  89.         Output.TextureUV = vTexCoord0; 
  90.     else
  91.         Output.TextureUV = 0; 
  92.     
  93.     return Output;    
  94. }
  95.  
  96.  
  97. //--------------------------------------------------------------------------------------
  98. // Pixel shader output structure
  99. //--------------------------------------------------------------------------------------
  100. struct PS_OUTPUT
  101. {
  102.     float4 RGBColor : COLOR0;  // Pixel color    
  103. };
  104.  
  105.  
  106. //--------------------------------------------------------------------------------------
  107. // This shader outputs the pixel's color by modulating the texture's
  108. //       color with diffuse material color
  109. //--------------------------------------------------------------------------------------
  110. PS_OUTPUT RenderScenePS( VS_OUTPUT In,
  111.                          uniform bool bTexture ) 
  112.     PS_OUTPUT Output;
  113.  
  114.     // Lookup mesh texture and modulate it with diffuse
  115.     if( bTexture )
  116.         Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
  117.     else
  118.         Output.RGBColor = In.Diffuse;
  119.  
  120.     return Output;
  121. }
  122.  
  123.  
  124. //--------------------------------------------------------------------------------------
  125. // Renders scene to render target
  126. //--------------------------------------------------------------------------------------
  127. technique RenderSceneWithTexture1Light
  128. {
  129.     pass P0
  130.     {          
  131.         VertexShader = compile vs_1_1 RenderSceneVS( 1, true, true );
  132.         PixelShader  = compile ps_1_1 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
  133.     }
  134. }
  135.  
  136. technique RenderSceneWithTexture2Light
  137. {
  138.     pass P0
  139.     {          
  140.         VertexShader = compile vs_1_1 RenderSceneVS( 2, true, true );
  141.         PixelShader  = compile ps_1_1 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
  142.     }
  143. }
  144.  
  145. technique RenderSceneWithTexture3Light
  146. {
  147.     pass P0
  148.     {          
  149.         VertexShader = compile vs_1_1 RenderSceneVS( 3, true, true );
  150.         PixelShader  = compile ps_1_1 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
  151.     }
  152. }
  153.  
  154. technique RenderSceneNoTexture
  155. {
  156.     pass P0
  157.     {          
  158.         VertexShader = compile vs_1_1 RenderSceneVS( 1, false, false );
  159.         PixelShader  = compile ps_1_1 RenderScenePS( false ); // trivial pixel shader (could use FF instead if desired)
  160.     }
  161. }
  162.